//
//  ViewController.m
//  OA
//
//  Created by apple on 15-10-15.
//  Copyright (c) 2015年 net. All rights reserved.
//

#import "ViewController.h"
#import "Constants.h"

@interface ViewController ()
{
    NSArray * r;
}
@end

@implementation ViewController
int prewTag;
float prewMoveY;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.appDelegate = [UIApplication sharedApplication].delegate;
    [self.loginButton.layer setCornerRadius:10.0];
    self.nameField.delegate = self;
    self.passwordField.delegate = self;
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFrame = textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.view.frame.size.height-textY;
    if (bottomY>=KeyBorderHeight) {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY = KeyBorderHeight-bottomY;
    prewMoveY=moveY;
    
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    frame.origin.y -= moveY;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    if (prewTag==-1) {
        return;
    }
    float moveY;
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    if (prewTag==textField.tag) {
        moveY = prewMoveY;
        frame.origin.y +=moveY;
    }
    
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return NO;//开始不允许跳转，只有当验证账号和密码正确可以进入后由登录代码执行切换
}


- (IBAction)backTapSuperView:(id)sender {
    [self.nameField resignFirstResponder];
    [self.passwordField resignFirstResponder];
}

- (IBAction)login:(id)sender {
    NSString * name = self.nameField.text;
    NSString * password = self.passwordField.text;
    if(name != nil && name.length>0
       && password !=nil && password.length>0)
    {
        //        if ([name isEqualToString:@"yilin"]&&[password isEqualToString:@"123"]) {
        //            [self performSegueWithIdentifier:@"loginSegue" sender:self];
        //        }else{
        //            UIAlertView * alert = [[UIAlertView alloc]
        //                                initWithTitle:@"提示"
        //                                message:@"用户名或密码错误"
        //                                delegate:nil
        //                                cancelButtonTitle:@"确定"
        //                                otherButtonTitles:nil];
        //            [alert show];
        //        }
        NSDictionary * params = @{@"name":name,@"pass":password};
        [self.appDelegate.manager
         POST:urlLogin
         parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             r = responseObject;
             NSDictionary * dict = [r objectAtIndex:0];
             NSString * result = [dict objectForKey:@"r"];
             if([result isEqualToString:@"success"]){
                 [self performSegueWithIdentifier:@"loginSegue" sender:self];
             }else{
                 UIAlertView * alert = [[UIAlertView alloc]
                                        initWithTitle:@"提示"
                                        message:@"用户名或密码错误"
                                        delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
                 [alert show];
             }
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             //NSLog(@"获取服务器响应出错！");
             UIAlertView * alert = [[UIAlertView alloc]
                                    initWithTitle:@"提示"
                                    message:@"获取服务器响应出错！"
                                    delegate:nil
                                    cancelButtonTitle:@"确定"
                                    otherButtonTitles:nil];
             [alert show];
         }];
    }
}

@end
